package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

public class PeakStrongHoldRecycleStrategy extends AbstractItemRecycleStrategy {
    private static Map<Integer, Integer> recycleItemMap = new HashMap<>();
    @Override
    public void markNeedRecycleItem() {
        //鎏金矿石
        recycleItemMap.put(eItemId.PEAKSTRONGHOLD_MINERAL_ITEM.getValue(), 1);
        //淘汰赛财神助力所需消耗道具
        recycleItemMap.put(eItemId.PEAKSTRONGHOLD_MAMMON_YIZHIQIANJIN.getValue(), 2);
        //沙漠宝库沙铲道具ID
        recycleItemMap.put(eItemId.PEAKSTRONGHOLD_SHOVEL_ITEM.getValue(), 4);
        PeakStrongHoldConfig config = PeakStrongholdActivityMgr.getConfig();
        if(config != null){
            ActivityInfo activityInfo = config.getActivityInfo();
            if (activityInfo != null) {
                for (Map.Entry<Integer, Integer> entry : recycleItemMap.entrySet()) {
                    Integer itemId = entry.getKey();
                    Integer type = entry.getValue();
                    if (type == 1 || type == 2) {
                        //领奖期以后回收的：财神助力 捐献材料
                        long recycleTime = activityInfo.getEndTime() * 1000L + 10 * 60 * 1000;
                        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleTime);
                    } else {
                        //活动入口不见后才回收的 沙漠宝库铲子
                        long recycleTime = activityInfo.getEndShowTime() * 1000L + 10 * 60 * 1000;
                        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleTime);
                    }
                }
            }
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.PeakStrongHold.getValue();
    }

    public int getType(int itemId) {
        return recycleItemMap.get(itemId);
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        int type = getType(itemId);
        Property reward = new Property();
        Map<String, Long> returnItemMap = new HashMap<>();
        UserBagItem bagItem = getUserBagItem(userId, itemId);
        if (bagItem != null && bagItem.getNum() > 0) {
            long num = bagItem.getNum();
            if (removeItem(bagItem)) {
                item2Reward(itemId, num, type, reward, returnItemMap);
            }
        }
        if (returnItemMap.size() > 0 && !reward.isNothing()) {
            sendEmail(type,userId, reward, returnItemMap);
        }
    }

    private void sendEmail(int type ,long userId, Property reward, Map<String, Long> returnItemMap) {
        String titleKey = MailManager.PEAKSTRONGHOLD_RETURN_ITEM_TITLE;
        String contentKey = MailManager.PEAKSTRONGHOLD_RETURN_ITEM_CONTENT;
        if(type == 2){
            titleKey = MailManager.PEAKSTRONGHOLD_RETURN_MAMMON_ITEM_TITLE;
            contentKey = MailManager.PEAKSTRONGHOLD_RETURN_MAMMON_ITEM_CONTENT;
        }

        String language = UserMgr.getLanguage(userId);
        StringBuilder returnStr = new StringBuilder();
        for (Map.Entry<String, Long> entry : returnItemMap.entrySet()) {
            String key = entry.getKey();
            Long value = entry.getValue();
            returnStr.append(ServerLanguageMgr.getContent(key, language)).append("X").append(value).append(", ");
        }
        returnStr.deleteCharAt(returnStr.length() - 1);
        String title = MultipleLanguageMgr.getContent(titleKey, language);
        String content = StringUtils.format(MultipleLanguageMgr.getContent(contentKey, language),
                returnStr.toString());
        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), content, title);
    }



    private void item2Reward(int itemId, long num, int type, Property reward, Map<String, Long> returnItemMap) {
        GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(itemId);
        if (type == 1) {
            for (long i = 0; i < num; i++) {
//                String extendParam = goodsInfo.getExtendParam();
//                Property property = RandomWeightConfigHelper.getRandomProperty(extendParam);
                Property useReward = PeakStrongholdActivityMgr.getMammonUseReward(itemId);
                reward.addProperty(useReward);
            }
        } else if (type == 2) {
            for (int i = 0; i < num; i++) {
                Property useReward = PeakStrongholdActivityMgr.getMammonUseReward(itemId);
                reward.addProperty(useReward);
            }
        }
        returnItemMap.put(goodsInfo.getGoodsName(), num);
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
